package com.jinzhi.demo.controller;

import java.net.HttpURLConnection;
import java.net.URL;
import java.util.Random;
import java.util.concurrent.CountDownLatch;

import org.apache.http.HttpEntity;
import org.apache.http.client.config.RequestConfig;
import org.apache.http.client.methods.CloseableHttpResponse;
import org.apache.http.client.methods.HttpPost;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.util.EntityUtils;

public class readNum {

    public static void main(String[] args) throws InterruptedException {


        final CountDownLatch countDownLatch = new CountDownLatch(80) ;
        for(int i=0;i<80;i++){
            final int num = i ;
            new Thread(new Runnable() {
                public void run() {
                    String url="https://ts.51rxzc.com/Labor/index.php/Second/fenxiang2?id=9564&user_id=";//张怡：9564/25981   彭俊：9559/19331
                    //String url2="https://ts.51rxzc.com/Labor/index.php/Second/fenxiang2?id=9559&user_id=";
                    String ip="172.43.1.";
                    Random r = new Random(1);
                    int t=2;
                    for(int a=100001*(num+1),j=1;a<100001*(num+1)+100001;a++,j++) {
                        go(url+a,ip+j);
                        //go(url2+i,ip+j);
                        System.out.println("访问地址:" + url+a);
                        //System.out.println("访问地址:" + url2+i);
//            			if(i%10==0) {
//            				int sleepTime=r.nextInt(5);
//            				System.out.println("等待" + sleepTime+"秒");
//            				Thread.sleep(sleepTime*1000);
//            			}
                        if(j==254) {
                            if(t>=255) {
                                t=0;
                            }
                            ip="172.43."+t+".";
                            t+=1;
                            j=1;
                        }
                    }
                }
            }).start();
        }
        // TODO Auto-generated method stub
        String url="https://ts.51rxzc.com/Labor/index.php/First/first";//张怡：9564/25981   彭俊：9559/19331
        //String url2="https://ts.51rxzc.com/Labor/index.php/Second/fenxiang2?id=9559&user_id=";
        String ip="172.56.100.";
//		for(int i=111660,j=1;i<466666;i++,j++) {
//			go(url+i,ip+j);
//			//go(url2+i,ip+j);
//			System.out.println("访问地址:" + url+i);
//			//System.out.println("访问地址:" + url2+i);
////			if(i%10==0) {
////				int sleepTime=r.nextInt(5);
////				System.out.println("等待" + sleepTime+"秒");
////				Thread.sleep(sleepTime*1000);
////			}
//			if(j==254) {
//				ip="172.56."+t+".";
//				t+=1;
//				j=1;
//			}
//		}
    }
    public static String go(String url,String ip) {
        try {

//            URL serverUrl = new URL(url);
//            HttpPost httpPost = new HttpPost(url);
//            HttpURLConnection conn = (HttpURLConnection) serverUrl
//                    .openConnection();
//            conn.setRequestMethod("GET");
//            // 必须设置false，否则会自动redirect到Location的地址
//            conn.setInstanceFollowRedirects(false);
//
//            conn.addRequestProperty("Accept-Charset", "UTF-8;");
//            conn.addRequestProperty("User-Agent",
//                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
//            conn.addRequestProperty("Referer", "http://zuidaima.com/");
//            conn.connect();
//            String location = conn.getHeaderField("Location");
//
//            serverUrl = new URL(location);
//            conn = (HttpURLConnection) serverUrl.openConnection();
//            conn.setRequestMethod("GET");
//
//            conn.addRequestProperty("Accept-Charset", "UTF-8;");
//            conn.addRequestProperty("User-Agent",
//                    "Mozilla/5.0 (Windows; U; Windows NT 5.1; zh-CN; rv:1.9.2.8) Firefox/3.6.8");
//            conn.addRequestProperty("Referer", "http://zuidaima.com/");
//            conn.connect();
//            System.out.println("跳转地址:" + location);
            CloseableHttpClient httpClient = HttpClients.createDefault();
            HttpPost httpPost = new HttpPost(url);
            /* 设置超时 */
            RequestConfig defaultRequestConfig = RequestConfig.custom().setSocketTimeout(5000).setConnectTimeout(5000).setConnectionRequestTimeout(5000).build();
            httpPost.setConfig(defaultRequestConfig);
            httpPost.addHeader("Content-Type", "application/json;charset=UTF-8");
            httpPost.addHeader("x-forwarded-for",ip);
            CloseableHttpResponse response = null;
            String result = null;
            try {
                response = httpClient.execute(httpPost);
                HttpEntity entity = response.getEntity();
                result = EntityUtils.toString(entity, "UTF-8");
            } catch (Exception e) {
                throw e;
            } finally {
                if (response != null) response.close();
                httpClient.close();
            }
            return result;


        } catch (Exception e) {
            e.printStackTrace();
        }
        return "访问失败";
    }

}
